package login;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name = "loginservlet", urlPatterns = {"/LoginPage/loginservlet"})
public class loginservlet extends HttpServlet {

    String username;
    String password;
    String identity1;
    String identity2;
    ResultSet rs;
    int count;
    String name;
    String pwd;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        username = request.getParameter("username");
        password = request.getParameter("password");
        identity1 = request.getParameter("identity");
        identity2 = (String) request.getSession().getAttribute("randonString");

        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection con
                    = DriverManager.getConnection("jdbc:sqlserver://114.32.112.40:168;databasename=NorthWind", "leagueofpg", "gjun");
            Statement st = con.createStatement();
            String sql = "Select 客戶編號 ,客戶密碼 from 客戶 where 客戶編號='" + username + "'";
            System.out.println(sql);
            rs = st.executeQuery(sql);

        } catch (Exception e) {
            System.out.println("帳號未輸入");
            request.setAttribute("error", "帳號未輸入");
            //response.sendRedirect("LoginPage/login.jsp");
            request.getRequestDispatcher("/LoginPage/login.jsp").forward(request, response);
            return;
        }
        try {
            rs.next();
            count = rs.getRow();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (count == 0) {
            System.out.println("帳號不存在");
            request.setAttribute("error", "帳號不存在");
            request.getRequestDispatcher("/LoginPage/login.jsp").forward(request, response);
        } else {
            System.out.println("帳號存在");
        }

        try {
            name = rs.getString("客戶編號").trim();
            pwd = rs.getString("客戶密碼").trim();
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (username.equals(name) && (password.equals(pwd))) {
            if (identity1.equals(identity2)) {
                System.out.println("驗證碼成功");
                request.getSession().setAttribute("username", username);
                request.getSession().setAttribute("password", password);
                request.getRequestDispatcher("/LoginPage/Success/success.jsp").forward(request, response);
            } else {
                System.out.println("驗證碼失敗");
                System.out.println(identity1);
                System.out.println(identity2);
                request.setAttribute("error", "驗證碼失敗");
                request.getRequestDispatcher("/LoginPage/login.jsp").forward(request, response);
            }
        } else {
//            System.out.println(username.equals(name));
//            System.out.println(password.equalsIgnoreCase(pwd));
//            System.out.println("1:"+password);
//            System.out.println("2:"+pwd);
            System.out.println("密碼錯誤");
            request.setAttribute("error", "密碼錯誤");
            request.getRequestDispatcher("/LoginPage/login.jsp").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
